package com.game.animation;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.game.config.Config;
import com.game.utils.AnimationInterpolatorUtil;
import com.game.utils.Tools;

/**
* @author oven:
* @version 创建时间：2012-11-28 上午10:43:09
* 渐变动画
*/
public abstract class AnimationGradualchange{
	protected Paint paint = Tools.createNewPaint();
	protected AnimationObject object;
	protected int type;//动画类型
	protected float time_step=0;//动画已播放的时间
	protected float time;//动画时间
	protected boolean hide = true;//出现 或 消失
	protected AnimationGradualchangeCallBack callBack;//动画完成的回调
	protected AnimationGradualchange next=null;//下一个动画
	protected int speedType=0;//速率类型
	protected int color = Color.BLACK;
	
	public AnimationGradualchange(AnimationObject object,AnimationGradualchangeCallBack callBack,float time){
		time=time*Config.FPS/1000f;
		this.time=time;
		this.object=object;
		this.callBack=callBack;
		this.object.addGradualChange(this);
		this.object.gradualchangIng=true;
		paint.setColor(color);
	}
	
	
	public void addGradualchange(AnimationGradualchange next){
		if(this.next==null){
			this.next=next;
		}else{
			next.addGradualchange(next);
		}
	}
	
	/**
	 * 迭代动画
	 * @param canvas
	 */
	public void stepGradulchange(Canvas canvas){
		//子类实现的方法
		step(canvas,calTimePerc());
		Log.i("infos", "正在绘制渐变动画");
		if(time_step>=time){
			if(callBack!=null){
				callBack.CallBack(this);
			}
			object.gradualchange=next;
			if(next==null){
				object.gradualchangIng=false;
			}
		}
		time_step++;
	}
	
	/**
	 * 根据动画时间转化变化量
	 * @return
	 */
	public float calTimePerc(){
		float input = time_step/time;
		float perc = input;
		switch (speedType) {
		case 0:{
			//匀速
			break;
		}
		case 1:{
			//加速
			perc=AnimationInterpolatorUtil.getAccelerate(input);
			break;
		}
		case 2:{
			//减速
			perc=AnimationInterpolatorUtil.getDecelerate(input);
			break;
		}
		case 3:{
			//加速后减速
			perc=AnimationInterpolatorUtil.getAccelerateDecelerate(input);
			break;
		}
		default:
			break;
		}
		
		if(!hide){
			perc=1-perc;
		}
		
		return perc;
	}
	
	public abstract void step(Canvas canvas,float perc);

	public boolean isHide() {
		return hide;
	}

	public void setHide(boolean hide) {
		this.hide = hide;
	}


	public AnimationGradualchangeCallBack getCallBack() {
		return callBack;
	}


	public int getSpeedType() {
		return speedType;
	}


	public void setCallBack(AnimationGradualchangeCallBack callBack) {
		this.callBack = callBack;
	}


	public void setSpeedType(int speedType) {
		this.speedType = speedType;
	}


	public int getColor() {
		return color;
	}


	public void setColor(int color) {
		this.color = color;
	}
		
}


